import { EventManager } from "../engine/EventManager";
import { GameSocket } from "../engine/GameSocket";
import { Logger } from "../engine/Logger";
import { EventConfig } from "../game/config/EventConfig";
import { RoomConfig } from "../game/config/RoomConfig";
import RoomConfig_Mj from "../game/config/RoomConfig_Mj";
import RedDotManager from "../game/manager/RedDotManager";
import { ClubShenheModel } from "../game/model/ClubShenheModel";
import { UserModel } from "../game/model/UserModel";
import { UIHelper } from "../game/util/UIHelper";
import { JhaoProto } from "../lib/net/protobuf/jhaoproto"
import { ProtoManager, WsSendData } from "../lib/net/protobuf/ProtoManager";

export class ClubSocketClient {


    public static onSocketRespMsg(baseMsg: JhaoProto.BaseMsg) {
        if (baseMsg.cmdIndex == JhaoProto.CmdIndex.club_list) {
            this.resp_club_list(baseMsg)
        }
        else if (baseMsg.cmdIndex == JhaoProto.CmdIndex.club_apply_list) {
            this.resp_club_apply_list(baseMsg)
        }
    }


    public static req_club_list() {
        let send_data: JhaoProto.Ireq_club_list = {
        }
        let send_obj: WsSendData = {
            cmd_index: JhaoProto.CmdIndex.club_list,
            "en_proto_ctor": JhaoProto.req_club_list,
            "data": send_data,
            "callback": (respData: JhaoProto.BaseMsg) => {
                if (respData.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                    this.resp_club_list(respData)
                } else {
                    UIHelper.showTip(respData.errorMsg)
                }
            }
        }
        GameSocket.getInstance().sendToSocket(send_obj)
    }

    private static resp_club_list(baseMsg: JhaoProto.BaseMsg) {
        let respProto: JhaoProto.resp_club_list = ProtoManager.instance.decode(JhaoProto.resp_club_list, baseMsg.data)
        UserModel.instance.clubList = respProto.clubs
        Logger.log("resp_club_list=", respProto)
        EventManager.instance.dispatch(EventConfig.Event_Update_Club_List)
    }

    public static req_club_create(name: string, notice: string = "禁止赌博,祝您游戏愉快！") {
        let send_data: JhaoProto.Ireq_club_create = {
            name: name,
            notice: notice
        }
        let send_obj: WsSendData = {
            cmd_index: JhaoProto.CmdIndex.club_create,
            "en_proto_ctor": JhaoProto.req_club_create,
            "data": send_data,
            "callback": (respData: JhaoProto.BaseMsg) => {
                Logger.log("req_club_create resp==data=", respData)
                if (respData.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                    let respProto: JhaoProto.resp_club_create = ProtoManager.instance.decode(JhaoProto.resp_club_create, respData.data)
                    Logger.log("req_club_create resp=222=data=", respProto)
                    UserModel.instance.clubList = respProto.clubs
                    EventManager.instance.dispatch(EventConfig.Event_Update_Club_List)
                } else {
                    UIHelper.showTip(respData.errorMsg)
                }
            }
        }
        GameSocket.getInstance().sendToSocket(send_obj)
    }

    public static req_club_join(clubid: number, callback: Function) {
        let send_data: JhaoProto.Ireq_club_join = {
            clubid: clubid,
        }
        let send_obj: WsSendData = {
            cmd_index: JhaoProto.CmdIndex.club_join,
            "en_proto_ctor": JhaoProto.req_club_join,
            "data": send_data,
            "callback": (respData: JhaoProto.BaseMsg) => {
                Logger.log("req_club_join resp==data=", respData)
                if (respData.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                    UIHelper.showTip("申请已提交，请等待管理员的审核")
                    callback && callback()
                } else {
                    UIHelper.showTip(respData.errorMsg)
                }
            }
        }
        GameSocket.getInstance().sendToSocket(send_obj)
    }

    public static req_club_apply_list(clubid: number, callback: Function) {
        let send_data: JhaoProto.Ireq_club_apply_list = {
            clubid: clubid,
        }
        let send_obj: WsSendData = {
            cmd_index: JhaoProto.CmdIndex.club_apply_list,
            "en_proto_ctor": JhaoProto.req_club_apply_list,
            "data": send_data,
            "callback": (respData: JhaoProto.BaseMsg) => {
                if (respData.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                    this.resp_club_apply_list(respData)
                    callback && callback()
                } else {
                    UIHelper.showTip(respData.errorMsg)
                }
            }
        }
        GameSocket.getInstance().sendToSocket(send_obj)
    }

    private static resp_club_apply_list(baseMsg: JhaoProto.BaseMsg) {
        let respProto: JhaoProto.resp_club_apply_list = ProtoManager.instance.decode(JhaoProto.resp_club_apply_list, baseMsg.data)
        ClubShenheModel.modelList = respProto.users
        Logger.log("resp_club_apply_list", respProto)
        RedDotManager.instance.checkShowReddotList([RedDotManager.RedDot_Type.Club_shenhe, RedDotManager.RedDot_Type.Club_shenhe2])
    }

    public static req_club_apply_handle(id: number, isAgree: boolean, clubid: number, userid: number, callback: Function) {
        let send_data: JhaoProto.Ireq_club_apply_handle = {
            id: id,
            isAgree: isAgree,
            clubid: clubid,
            userid: userid,
        }
        let send_obj: WsSendData = {
            cmd_index: JhaoProto.CmdIndex.club_apply_handle,
            "en_proto_ctor": JhaoProto.req_club_apply_handle,
            "data": send_data,
            "callback": (respData: JhaoProto.BaseMsg) => {
                Logger.log("req_club_apply_handle resp==data=", respData)
                if (respData.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                    callback && callback()
                } else {
                    UIHelper.showTip(respData.errorMsg)
                }
            }
        }
        GameSocket.getInstance().sendToSocket(send_obj)
    }


    public static req_club_exit(clubid: number, callback: Function) {
        let send_data: JhaoProto.Ireq_club_join = {
            clubid: clubid,
        }
        let send_obj: WsSendData = {
            cmd_index: JhaoProto.CmdIndex.club_exit,
            "en_proto_ctor": JhaoProto.req_club_exit,
            "data": send_data,
            "callback": (respData: JhaoProto.BaseMsg) => {
                if (respData.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                    callback && callback()
                } else {
                    UIHelper.showTip(respData.errorMsg)
                }
            }
        }
        GameSocket.getInstance().sendToSocket(send_obj)
    }

    public static req_club_member_list(clubid: number, callback: Function) {
        let send_data: JhaoProto.Ireq_club_member_list = {
            clubid: clubid,
        }
        let send_obj: WsSendData = {
            cmd_index: JhaoProto.CmdIndex.club_member_list,
            "en_proto_ctor": JhaoProto.req_club_member_list,
            "data": send_data,
            "callback": (baseMsg: JhaoProto.BaseMsg) => {
                if (baseMsg.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                    let respPack: JhaoProto.resp_club_member_list = ProtoManager.instance.decode(JhaoProto.resp_club_member_list, baseMsg.data)
                    Logger.log("req_club_member_list==", respPack)
                    callback && callback(respPack.users)
                } else {
                    UIHelper.showTip(baseMsg.errorMsg)
                }
            }
        }
        GameSocket.getInstance().sendToSocket(send_obj)
    }

    public static req_club_delete_member(clubid: number, userid: number, callback: Function) {
        let send_data: JhaoProto.Ireq_club_delete_member = {
            clubid: clubid,
            userid: userid
        }
        let send_obj: WsSendData = {
            cmd_index: JhaoProto.CmdIndex.club_delete_member,
            "en_proto_ctor": JhaoProto.req_club_delete_member,
            "data": send_data,
            "callback": (respData: JhaoProto.BaseMsg) => {
                if (respData.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                    callback && callback()
                } else {
                    UIHelper.showTip(respData.errorMsg)
                }
            }
        }
        GameSocket.getInstance().sendToSocket(send_obj)
    }

    public static req_club_member_actor(clubid: number, userid: number, actor: number, callback: Function) {
        let send_data: JhaoProto.Ireq_club_member_actor = {
            clubid: clubid,
            userid: userid,
            actor: actor
        }
        let send_obj: WsSendData = {
            cmd_index: JhaoProto.CmdIndex.club_member_actor,
            "en_proto_ctor": JhaoProto.req_club_member_actor,
            "data": send_data,
            "callback": (respData: JhaoProto.BaseMsg) => {
                if (respData.errorCode == JhaoProto.ErrorCodeWs.Suc) {

                    callback && callback()
                } else {
                    UIHelper.showTip(respData.errorMsg)
                }
            }
        }
        GameSocket.getInstance().sendToSocket(send_obj)
    }

    public static req_club_score(clubid: number, userid: number, score: number, callback: Function) {
        let send_data: JhaoProto.Ireq_club_score = {
            clubid: clubid,
            userid: userid,
            score: score
        }
        let send_obj: WsSendData = {
            cmd_index: JhaoProto.CmdIndex.club_score,
            "en_proto_ctor": JhaoProto.req_club_score,
            "data": send_data,
            "callback": (respData: JhaoProto.BaseMsg) => {
                if (respData.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                    callback && callback()
                } else {
                    UIHelper.showTip(respData.errorMsg)
                }
            }
        }
        GameSocket.getInstance().sendToSocket(send_obj)
    }


    public static req_club_create_room_template(clubid: number, name: string, roomConfig: RoomConfig_Mj | RoomConfig, callback: Function = null) {
        let gameType: number = roomConfig.gameType
        let roomType: number = roomConfig.roomType
        let roomConfigBase: JhaoProto.IRoomConfigBase = {
            renshu: roomConfig.renshu,
            jushu: roomConfig.jushu,
            cost: roomConfig.cost,
            costType: roomConfig.cost_type,
            difen:roomConfig.difen,
            distanceLimit: roomConfig.distance_limit,
            dismissTime: roomConfig.dismiss_time,
            tuoguanTime: roomConfig.tuoguan_time,
            limitEmoji: roomConfig.limit_emoji,
            limitVoice: roomConfig.limit_voice,
            limitDropItem: roomConfig.limitDropItem
        }
        let roomConfigMj: JhaoProto.IRoomConfigMj = {
        }
        if (roomConfig instanceof RoomConfig_Mj) {
            roomConfigMj.dianpaoType = roomConfig.dianpao_type;
        }
        let send_data: JhaoProto.Ireq_club_create_room_template = {
            clubid: clubid,
            name: name,
            gameType: gameType,
            roomType: roomType,
            roomConfigBase: roomConfigBase,
            roomConfigMj: roomConfigMj
        }

        let send_obj: WsSendData = {
            cmd_index: JhaoProto.CmdIndex.club_create_room_template,
            "en_proto_ctor": JhaoProto.req_club_create_room_template,
            "data": send_data,
            "callback": (respData: JhaoProto.BaseMsg) => {
                if (respData.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                    callback && callback()
                } else {
                    UIHelper.showTip(respData.errorMsg)
                }
            }
        }
        Logger.log("req_club_create_room_template==", send_obj)
        GameSocket.getInstance().sendToSocket(send_obj)

    }

    public static req_club_delete_room_template(id:number, clubid:number, callback:Function){
        let send_data: JhaoProto.Ireq_club_delete_room_template = {
            id:id,
            clubid: clubid,
        }
        let send_obj: WsSendData = {
            cmd_index: JhaoProto.CmdIndex.club_delete_room_template,
            "en_proto_ctor": JhaoProto.req_club_delete_room_template,
            "data": send_data,
            "callback": (baseMsg: JhaoProto.BaseMsg) => {
                if (baseMsg.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                    callback && callback()
                } else {
                    UIHelper.showTip(baseMsg.errorMsg)
                }
            }
        }
        Logger.log("req_club_delete_room_template", send_obj)
        GameSocket.getInstance().sendToSocket(send_obj)
    }
    

    public static req_club_room(clubid: number, callback:Function=null){
        let send_data: JhaoProto.Ireq_club_room = {
            clubid: clubid,
        }
        let send_obj: WsSendData = {
            cmd_index: JhaoProto.CmdIndex.club_room,
            "en_proto_ctor": JhaoProto.req_club_room,
            "data": send_data,
            "callback": (baseMsg: JhaoProto.BaseMsg) => {
                if (baseMsg.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                    let respPack: JhaoProto.resp_club_room = ProtoManager.instance.decode(JhaoProto.resp_club_room, baseMsg.data)
                    // Logger.log("req_club_room=resp=", respPack)
                    callback && callback(respPack)
                } else {
                    UIHelper.showTip(baseMsg.errorMsg)
                }
            }
        }
        // Logger.log("req_club_room==", send_obj)
        GameSocket.getInstance().sendToSocket(send_obj)
    }


}